﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebBags.Models;
using WebBags.Common;

namespace WebBags.Controllers
{
    public class AdminController : Controller
    {
        private DBWebBagsDataContext db = new DBWebBagsDataContext();

        //[Authorize(Roles = "Admin")]
        public ActionResult ListCategory()
        {
            List<Category> lst = db.Categories.ToList();
            return View(lst);
        }

        //[Authorize(Roles = "Admin")]
        public ActionResult DelCategory(long id)
        {
            Category c = db.Categories.Single(x => x.ID == id);
            db.Categories.DeleteOnSubmit(c);
            db.SubmitChanges();
            return RedirectToAction("ListCategory");
        }

        //[Authorize(Roles = "Admin")]
        public ActionResult AddCategory()
        {
            return View();
        }

        [HttpPost]
        //[Authorize(Roles = "Admin")]
        public ActionResult AddCategory(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.InsertOnSubmit(category);
                db.SubmitChanges();
                return RedirectToAction("ListCategory");
            }
            else
            {
                return View(category);
            }
        }

        //[Authorize(Roles = "Admin")]
        public ActionResult EditCategory(long id)
        {
            Category c = db.Categories.Single(x => x.ID == id);
            return View(c);
        }

        [HttpPost]
        //[Authorize(Roles = "Admin")]
        public ActionResult EditCategory(Category category)
        {
            if (ModelState.IsValid)
            {
                Category c = db.Categories.Single(x => x.ID == category.ID);
                c.Name_en = category.Name_en;
                c.Name_vn = category.Name_vn;
                db.SubmitChanges();
                return RedirectToAction("ListCategory");
            }
            else
            {
                return View(category);
            }
        }

        //[Authorize(Roles = "Admin")]
        public ActionResult ListProduct()
        {
            string strPage = string.IsNullOrEmpty(Request.Params["paging"]) || !MyValidation.ValidateNumber(Request.Params["paging"]) ? "1" : Request.Params["paging"];
            int intPageSize = 10;
            int intPos = (int.Parse(strPage) * intPageSize - intPageSize);
            var lst = db.Products;
            ViewData["intSum"] = lst.Count();
            ViewData["paging"] = strPage;
            return View(lst.Skip(intPos).Take(intPageSize));            
        }
    }
}
